<%@ page import="databeans.*"%>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.*" %>
<%@ page import="helper.*"%>

<%
	Customer customer = (Customer)session.getAttribute("customer");
	String customerUsername = customer.getUsername();
	String customerFirstName = customer.getFirstName();
	String customerLastName = customer.getLastName();
%>
<jsp:include page="../common/header.jsp" />

<script type="text/javascript">
	$(document).ready(function() { 
		var loading_html = "<img src=\"view/img/mozilla_blu.gif\"></img> Fetching Data... Please wait";
        $("#popup").dialog({ bgiframe: true, autoOpen:false, modal: true, closeOnEscape: true, draggable:false, width:700, height:500, position: 'top', buttons: { "Close": function() { $(this).dialog('close'); } } });

		$('.fund_graph').visualize({type: 'bar', parseDirection:'x', pieLabelPos:'outside', height: 230, width:520, colors:['#8CCE3B','#92d5ea','#ee8310','#be1e2d']});
		$('#colorize_1').colorize({ hoverColor:'none', ignoreHeaders :true, hiliteColor :'none' });
		$('#colorize_2').colorize({ hoverColor:'none', ignoreHeaders :true, hiliteColor :'none' });

		$('a#view-pending').click(function(e) {
			//alert($(this).attr('href'));
			var id = $(this).attr('href');
			var customer_id = id.substring(1,id.length);
			//alert(customer_id);
			e.preventDefault(); 
			$('#popup').html(loading_html);
			$('#popup').dialog('open'); 
			$.post(
				       
				    "customerViewPendingTransactions.do", //Ajax file
				    {'customerID': customer_id, 'type': "ajax" },
				    
				    //function that is called when server returns a value.
					    function(html){
					        $('#popup').html(html); // Populate Data through this line
				    	},
				    	"html"
				    );
		});
	});
</script>

<title>Carnegie Financial Services - Customer Dashboard</title>
</head>

<body>
	<div id="container">
    	<div id="header">
        	<h2>Carnegie Financial Services</h2>
    		<jsp:include page="../common/top-menu.jsp" />
      </div>
        <div id="wrapper">
            <div id="content">
           	 <jsp:include page="../common/messages.jsp"/>
       			<div id="rightnow">
                    <h3 class="reallynow">
                        <span>Welcome, <%=customerFirstName + " " + customerLastName %></span>
                        <br />
                    </h3>
				    <p class="youhave">You have <a href="#<%=customer.getCustomerID() %>" title="Click to view Pending orders" id="view-pending"><%=(Integer)request.getAttribute("noOfPendings") %> transactions</a> pending</p>
			  </div>
              	<div id="box" class="graph" style="height:350px;">
                    <h3>Fund Activity</h3>
                    <% 
	            		SimpleDateFormat format = new SimpleDateFormat("EEE M/d");
					  if((request.getAttribute("fundHistories")==null))
					  {
					  %>
					  	<p class="pad-left-20">There is no history of this Fund. </p>
					 <% 
					 }else{
	            		List<FundHistory[]> fundHistories = (ArrayList<FundHistory[]>) request.getAttribute("fundHistories");
	            		List<String> fundNames = (ArrayList<String>) request.getAttribute("fundNames");
						FundHistory[] fh = fundHistories.get(0);
				%>
                		<table class="fund_graph">
                			<thead>
                				<tr>
                					<td></td>
                				<%
                				for(int i =0;i<fh.length;i++){
                					
								%>
								 	<th><%=format.format(fh[i].getPriceDate()) %></th>
							  	<%} %>
							  	</tr>
							</thead>
							<tbody>
								<%
									for(int i=0; i<fundHistories.size();i++)
									{
										FundHistory[] fund = fundHistories.get(i);
								%>
   								 <tr>
   								 <th><%=fundNames.get(i) %></th>
   								 <%
									for(int j =0 ; j< fh.length ; j++)
									{
								%>
      									<td><%=helper.Formatter.prepCashToView(fund[j].getPrice()) %></td>
      							<%	} %>
								</tr>
								<%} %>
							</tbody>
						</table>
						<%
							}
						%>
	    			<div class="padding"></div>
                  </div>
                  <div id="infowrap">
	                  <div id="infobox">
	                    <h3>Recent Purchases</h3>
	                    <%
							ArrayList<Transaction> transactions_buy = (ArrayList<Transaction>)request.getAttribute("transactions_buy");
							ArrayList<String> fundNames_trans_buy = (ArrayList<String>)request.getAttribute("fundNames_trans_buy");
							System.out.println("customer-dashboard.jsp+ transctions "+transactions_buy==null);
							System.out.println("customer-dashboard.jsp+ fundNames "+fundNames_trans_buy==null);
							if(transactions_buy!=null){
							%>
		                    <table id="colorize_1">
							      <thead>
										<tr>
											<th>Type</th>
											<th>Fund</th>
											<th>Shares</th>
											<th>Value</th>
										</tr>
							      </thead>
							      <tbody>
							<%
							for(int i=0; i<transactions_buy.size(); i++){
							%>
							       <tr>
							       		<td><%=helper.Formatter.transactionType(transactions_buy.get(i).getTransactionType()) %></td>
		                             	<td><a href="#"><%=fundNames_trans_buy.get(i)%></a></td>
		                                <td><%=helper.Formatter.prepShareToView(transactions_buy.get(i).getShares()) %></td>
		                                <td>$<%=helper.Formatter.prepCashToView(transactions_buy.get(i).getAmount()) %></td>
		                            </tr>
							<%
							  }
							%>
								</tbody>
							</table>
							<%}else { %>
							<p class="pad-left-20">No Shares have been bought till date.</p>
							<%} %>
	                  </div>
	                  <div id="infobox" class="margin-left">
	                    <h3>Recent Sales</h3>
	                    <%
							ArrayList<Transaction> transactions_sell = (ArrayList<Transaction>)request.getAttribute("transactions_sell");
							ArrayList<String> fundNames_trans_sell = (ArrayList<String>)request.getAttribute("fundNames_trans_sell");
							System.out.println("customer-dashboard.jsp+ transctions "+transactions_sell==null);
							System.out.println("customer-dashboard.jsp+ fundNames "+fundNames_trans_sell==null);
							if(transactions_sell!=null){
							%>
		                    <table id="colorize_2">
							      <thead>
										<tr>
											<th>Type</th>
											<th>Fund</th>
											<th>Shares</th>
											<th>Value</th>
										</tr>
							      </thead>
							      <tbody>
							<%
							for(int i=0; i<transactions_buy.size(); i++){
							%>
							       <tr>
							       		<td><%=helper.Formatter.transactionType(transactions_sell.get(i).getTransactionType()) %></td>
		                             	<td><a href="#"><%=fundNames_trans_sell.get(i)%></a></td>
		                                <td><%=helper.Formatter.prepShareToView(transactions_sell.get(i).getShares()) %></td>
		                                <td>$<%=helper.Formatter.prepCashToView(transactions_sell.get(i).getAmount()) %></td>
		                            </tr>
							<%
							  }
							%>
								</tbody>
							</table>
							<%}else { %>
							<p class="pad-left-20">No Shares have been bought till date.</p>
							<%} %>
	                  </div>
                  </div><!-- close id=infowrap -->
            </div>
            <jsp:include page="../common/sidebar.jsp" />
      </div>
        <jsp:include page="../common/footer.jsp" />
</div>

<!-- html for jQuery Graph. Populate with values from DB -->
<div id="popup" title="Pending Transactions">
</div>
</body>
</html>
